home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / FileChooserDemoFrame.java < prev    next >
Text File  |  1998-11-01  |  12KB  |  374 lines

  1. /*
  2.         A GUI version of the FileChooser sample from SUN. 
  3.         
  4.         This frame sets the options for the JFileChooser
  5. */
  6.  
  7. import com.sun.java.swing.*;
  8. import com.sun.java.swing.preview.*;
  9. import com.sun.java.swing.preview.filechooser.*;
  10. import java.awt.*;
  11. import java.beans.*;
  12. import java.io.*;
  13. import com.symantec.itools.swing.JLookAndFeelComboBox;
  14.  
  15. public class FileChooserDemoFrame extends com.sun.java.swing.JFrame
  16. {
  17.     ButtonGroup group1 = new ButtonGroup();
  18.     ButtonGroup group2 = new ButtonGroup();
  19.     ButtonGroup group3 = new ButtonGroup();
  20.     public FileChooserDemoFrame()
  21.     {
  22.         // This code is automatically generated by Visual Cafe when you add
  23.         // components to the visual environment. It instantiates and initializes
  24.         // the components. To modify the code, only use code syntax that matches
  25.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  26.         // parse your Java file into its visual environment.
  27.         //{{INIT_CONTROLS
  28.         setTitle("File Chooser Demo");
  29.         getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
  30.         getContentPane().setFont(new Font("Dialog", Font.PLAIN, 0));
  31.         setSize(525,305);
  32.         setVisible(false);
  33.         JPanel1.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
  34.         getContentPane().add(JPanel1);
  35.         JPanel1.setBounds(0,0,525,61);
  36.         openOption.setText("Open");
  37.         openOption.setActionCommand("Open");
  38.         openOption.setSelected(true);
  39.         JPanel1.add(openOption);
  40.         openOption.setBounds(5,5,57,25);
  41.         saveOption.setText("Save");
  42.         saveOption.setActionCommand("Save");
  43.         JPanel1.add(saveOption);
  44.         saveOption.setBounds(67,5,55,25);
  45.         customOption.setText("Custom");
  46.         customOption.setActionCommand("Custom");
  47.         JPanel1.add(customOption);
  48.         customOption.setBounds(127,5,71,25);
  49.         customMessage.setText("DoIt");
  50.         customMessage.setColumns(20);
  51.         customMessage.setEnabled(false);
  52.         JPanel1.add(customMessage);
  53.         customMessage.setBounds(203,8,220,19);
  54.         customMessage.setSelectionEnd(4);
  55.         customMessage.setSelectionStart(4);
  56.         customMessage.setCaretPosition(4);
  57.         JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
  58.         getContentPane().add(JPanel2);
  59.         JPanel2.setBounds(0,61,525,61);
  60.         noFilterOption.setText("No Filtering");
  61.         noFilterOption.setActionCommand("No Filtering");
  62.         noFilterOption.setSelected(true);
  63.         JPanel2.add(noFilterOption);
  64.         noFilterOption.setBounds(5,5,90,25);
  65.         addFilterOption.setText("Add JPG and GIF Filters");
  66.         addFilterOption.setActionCommand("Add JPG and GIF Filters");
  67.         JPanel2.add(addFilterOption);
  68.         addFilterOption.setBounds(100,5,157,25);
  69.         previewOption.setText("Show Preview");
  70.         previewOption.setActionCommand("Show Preview");
  71.         previewOption.setSelected(true);
  72.         JPanel2.add(previewOption);
  73.         previewOption.setBounds(262,5,108,25);
  74.         JPanel3.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
  75.         getContentPane().add(JPanel3);
  76.         JPanel3.setBounds(0,122,525,61);
  77.         hiddenFileOption.setText("Show Hidden Files");
  78.         hiddenFileOption.setActionCommand("Show Hidden Files");
  79.         JPanel3.add(hiddenFileOption);
  80.         hiddenFileOption.setBounds(5,5,130,25);
  81.         extensionsOption.setText("Show Extensions");
  82.         extensionsOption.setActionCommand("Show Extensions");
  83.         extensionsOption.setSelected(true);
  84.         JPanel3.add(extensionsOption);
  85.         extensionsOption.setBounds(140,5,125,25);
  86.         fileViewOption.setText("Use FileView");
  87.         fileViewOption.setActionCommand("Use FileView");
  88.         fileViewOption.setSelected(true);
  89.         JPanel3.add(fileViewOption);
  90.         fileViewOption.setBounds(270,5,99,25);
  91.         JPanel4.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
  92.         getContentPane().add(JPanel4);
  93.         JPanel4.setBounds(0,183,525,61);
  94.         filesOption.setText("Just Select Files");
  95.         filesOption.setActionCommand("Just Select Files");
  96.         filesOption.setSelected(true);
  97.         JPanel4.add(filesOption);
  98.         filesOption.setBounds(5,5,120,25);
  99.         directoriesOption.setText("Just Select Directories");
  100.         directoriesOption.setActionCommand("Just Select Directories");
  101.         JPanel4.add(directoriesOption);
  102.         directoriesOption.setBounds(130,5,157,25);
  103.         filesAndDirectoriesOption.setText("Select files or Directories");
  104.         filesAndDirectoriesOption.setActionCommand("Select files or Directories");
  105.         JPanel4.add(filesAndDirectoriesOption);
  106.         filesAndDirectoriesOption.setBounds(292,5,171,25);
  107.         JPanel5.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
  108.         getContentPane().add(JPanel5);
  109.         JPanel5.setBounds(0,244,525,61);
  110.         showFileChooser.setText("Show FileChooser");
  111.         showFileChooser.setActionCommand("Show FileChooser");
  112.         showFileChooser.setMnemonic((int)'S');
  113.         JPanel5.add(showFileChooser);
  114.         showFileChooser.setBounds(141,5,137,25);
  115.         JPanel5.add(setLookAndFeelComboBox);
  116.         setLookAndFeelComboBox.setBounds(298,5,86,24);
  117.         //}}
  118.         
  119.         group1.add(openOption);
  120.         group1.add(saveOption);
  121.         group1.add(customOption);
  122.         
  123.         group2.add(noFilterOption);
  124.         group2.add(addFilterOption);
  125.         
  126.         group3.add(filesOption);
  127.         group3.add(directoriesOption);
  128.         group3.add(filesAndDirectoriesOption);
  129.  
  130.         //{{INIT_MENUS
  131.         //}}
  132.     
  133.         //{{REGISTER_LISTENERS
  134.         SymWindow aSymWindow = new SymWindow();
  135.         this.addWindowListener(aSymWindow);
  136.         SymItem lSymItem = new SymItem();
  137.         customOption.addItemListener(lSymItem);
  138.         SymAction lSymAction = new SymAction();
  139.         showFileChooser.addActionListener(lSymAction);
  140.         //}}
  141.     }
  142.  
  143.     static public void main(String args[])
  144.     {
  145.         JFrame jf=new FileChooserDemoFrame();
  146.         jf.setLocation(50,50);
  147.         jf.setVisible(true);
  148.     }
  149.  
  150.     //{{DECLARE_CONTROLS
  151.     com.sun.java.swing.JPanel JPanel1 = new com.sun.java.swing.JPanel();
  152.     com.sun.java.swing.JRadioButton openOption = new com.sun.java.swing.JRadioButton();
  153.     com.sun.java.swing.JRadioButton saveOption = new com.sun.java.swing.JRadioButton();
  154.     com.sun.java.swing.JRadioButton customOption = new com.sun.java.swing.JRadioButton();
  155.     com.sun.java.swing.JTextField customMessage = new com.sun.java.swing.JTextField();
  156.     com.sun.java.swing.JPanel JPanel2 = new com.sun.java.swing.JPanel();
  157.     com.sun.java.swing.JRadioButton noFilterOption = new com.sun.java.swing.JRadioButton();
  158.     com.sun.java.swing.JRadioButton addFilterOption = new com.sun.java.swing.JRadioButton();
  159.     com.sun.java.swing.JCheckBox previewOption = new com.sun.java.swing.JCheckBox();
  160.     com.sun.java.swing.JPanel JPanel3 = new com.sun.java.swing.JPanel();
  161.     com.sun.java.swing.JCheckBox hiddenFileOption = new com.sun.java.swing.JCheckBox();
  162.     com.sun.java.swing.JCheckBox extensionsOption = new com.sun.java.swing.JCheckBox();
  163.     com.sun.java.swing.JCheckBox fileViewOption = new com.sun.java.swing.JCheckBox();
  164.     com.sun.java.swing.JPanel JPanel4 = new com.sun.java.swing.JPanel();
  165.     com.sun.java.swing.JRadioButton filesOption = new com.sun.java.swing.JRadioButton();
  166.     com.sun.java.swing.JRadioButton directoriesOption = new com.sun.java.swing.JRadioButton();
  167.     com.sun.java.swing.JRadioButton filesAndDirectoriesOption = new com.sun.java.swing.JRadioButton();
  168.     com.sun.java.swing.JPanel JPanel5 = new com.sun.java.swing.JPanel();
  169.     com.sun.java.swing.JButton showFileChooser = new com.sun.java.swing.JButton();
  170.     com.symantec.itools.swing.JLookAndFeelComboBox setLookAndFeelComboBox = new com.symantec.itools.swing.JLookAndFeelComboBox();
  171.     //}}
  172.  
  173.     //{{DECLARE_MENUS
  174.     //}}
  175.  
  176.  
  177.     class SymWindow extends java.awt.event.WindowAdapter
  178.     {
  179.         public void windowClosing(java.awt.event.WindowEvent event)
  180.         {
  181.             Object object = event.getSource();
  182.             if (object == FileChooserDemoFrame.this)
  183.                 FileChooserDemoFrame_windowClosing(event);
  184.         }
  185.     }
  186.  
  187.     void FileChooserDemoFrame_windowClosing(java.awt.event.WindowEvent event)
  188.     {
  189.         System.exit(0);
  190.     }
  191.  
  192.     class SymItem implements java.awt.event.ItemListener
  193.     {
  194.         public void itemStateChanged(java.awt.event.ItemEvent event)
  195.         {
  196.             Object object = event.getSource();
  197.             if (object == customOption)
  198.                 customOption_itemStateChanged(event);
  199.         }
  200.     }
  201.  
  202.     void customOption_itemStateChanged(java.awt.event.ItemEvent event)
  203.     {
  204.         if(customOption.isSelected())
  205.         {
  206.             customMessage.setEnabled(true);
  207.         }
  208.         else
  209.         {
  210.             customMessage.setEnabled(false);
  211.         }
  212.     }
  213.  
  214.     class SymAction implements java.awt.event.ActionListener
  215.     {
  216.         public void actionPerformed(java.awt.event.ActionEvent event)
  217.         {
  218.             Object object = event.getSource();
  219.             if (object == showFileChooser)
  220.                 showFileChooser_actionPerformed(event);
  221.         }
  222.     }
  223.  
  224.     void showFileChooser_actionPerformed(java.awt.event.ActionEvent event)
  225.     {
  226.         JFileChooser fc = new JFileChooser();
  227.         
  228.         //Select Dialog type
  229.         if(openOption.isSelected())
  230.         {
  231.             fc.setDialogType(JFileChooser.OPEN_DIALOG);
  232.         }
  233.         else if (saveOption.isSelected())
  234.         {
  235.             fc.setDialogType(JFileChooser.SAVE_DIALOG);
  236.         }
  237.         else if (customOption.isSelected())
  238.         {
  239.             fc.setDialogType(JFileChooser.CUSTOM_DIALOG);
  240.             fc.setApproveButtonText(customMessage.getText());
  241.         }
  242.         
  243.         //setup for filters
  244.         if(addFilterOption.isSelected())
  245.         {
  246.             ExampleFileFilter jpgFilter=new ExampleFileFilter("jpg", "JPEG Compressed Image Files"), 
  247.                               gifFilter=new ExampleFileFilter("gif", "GIF Image Files"), 
  248.                               bothFilter=new ExampleFileFilter(new String[] {"jpg", "gif"}, "JPEG and GIF Image Files");
  249.  
  250.             //setup for extensions
  251.             jpgFilter.setExtensionListInDescription(extensionsOption.isSelected());
  252.             gifFilter.setExtensionListInDescription(extensionsOption.isSelected());
  253.             bothFilter.setExtensionListInDescription(extensionsOption.isSelected());
  254.             //add filters
  255.             fc.resetChoosableFileFilters();
  256.             fc.addChoosableFileFilter(jpgFilter);
  257.             fc.addChoosableFileFilter(gifFilter);
  258.             fc.addChoosableFileFilter(bothFilter);
  259.         }
  260.         
  261.         //setup for previewing
  262.         if(previewOption.isSelected())
  263.         {
  264.             fc.setAccessory(new FilePreviewer(fc));
  265.         }
  266.         
  267.         //setup for hidden files
  268.         fc.setFileHidingEnabled(!hiddenFileOption.isSelected());
  269.         
  270.         //setup for file view
  271.         if(fileViewOption.isSelected())
  272.         {
  273.             ExampleFileView fileView = new ExampleFileView();
  274.             fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
  275.             fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
  276.             fc.setFileView(fileView);
  277.         }
  278.         
  279.         //decide on acceptable selection types
  280.         if(filesOption.isSelected())
  281.         {
  282.             fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
  283.         }
  284.         else if(directoriesOption.isSelected())
  285.         {
  286.             fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  287.         }
  288.         else if(filesAndDirectoriesOption.isSelected())
  289.         {
  290.             fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  291.         }
  292.         
  293.         //Display the dialog, and tell the user which file he picked.
  294.         if (fc.showDialog(this, null ) == JFileChooser.APPROVE_OPTION)
  295.         {
  296.             File theFile = fc.getSelectedFile();
  297.             if(theFile!=null)
  298.             {
  299.                 if(theFile.isDirectory())
  300.                 {
  301.                     JOptionPane.showMessageDialog(this, "You chose this directory: " + theFile.getAbsolutePath() );
  302.                 }
  303.                 else
  304.                 {
  305.                     JOptionPane.showMessageDialog(this, "You chose this file: " + theFile.getAbsolutePath());
  306.                 }
  307.             }
  308.         }
  309.     }
  310.  
  311.     class FilePreviewer extends JComponent implements PropertyChangeListener {
  312.         ImageIcon thumbnail = null;
  313.         File f = null;
  314.  
  315.         public FilePreviewer(JFileChooser fc) 
  316.         {
  317.             setPreferredSize(new Dimension(100, 50));
  318.             fc.addPropertyChangeListener(this);
  319.         }
  320.  
  321.         public void loadImage() 
  322.         {
  323.             if(f != null) 
  324.             {
  325.                 ImageIcon tmpIcon = new ImageIcon(f.getPath());
  326.                 if(tmpIcon.getIconWidth() > 90) 
  327.                 {
  328.                     thumbnail = new ImageIcon(
  329.                     tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
  330.                 } 
  331.                 else 
  332.                 {
  333.                     thumbnail = tmpIcon;
  334.                 }
  335.             }
  336.         }
  337.  
  338.         public void propertyChange(PropertyChangeEvent e) 
  339.         {
  340.             String prop = e.getPropertyName();
  341.             if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) 
  342.             {
  343.                 f = (File) e.getNewValue();
  344.                 if(isShowing()) 
  345.                 {
  346.                     loadImage();
  347.                     repaint();
  348.                 }
  349.             }
  350.         }
  351.  
  352.         public void paint(Graphics g) {
  353.             if(thumbnail == null) 
  354.             {
  355.                 loadImage();
  356.             }
  357.             if(thumbnail != null) 
  358.             {
  359.                 int x = getWidth()/2 - thumbnail.getIconWidth()/2;
  360.                 int y = getHeight()/2 - thumbnail.getIconHeight()/2;
  361.                 if(y < 0) 
  362.                 {
  363.                     y = 0;
  364.                 }
  365.  
  366.                 if(x < 5) 
  367.                 {
  368.                     x = 5;
  369.                 }
  370.                 thumbnail.paintIcon(this, g, x, y);
  371.             }
  372.         }
  373.     }
  374. }